//
//  NewFeatureViewController.swift
//  Swiftsweibo
//
//  Created by 张鹏飞 on 16/1/9.
//  Copyright © 2016年 张鹏飞. All rights reserved.
//

import UIKit
import SnapKit

private let WBNewFeatureViewCellID = "WBNewFeatureViewCellID"
private let WBNewFeatureCount = 4

class NewFeatureViewController: UICollectionViewController {
    
/// 构造方法
    init () {
        
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //最小格子间距
        layout.minimumInteritemSpacing = 0
        //最小行间距
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        //指示器
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureViewCell.self, forCellWithReuseIdentifier: WBNewFeatureViewCellID)

        // Do any additional setup after loading the view.
    }
    /// 隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    // MARK: UICollectionViews数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return WBNewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBNewFeatureViewCellID, forIndexPath: indexPath) as! NewFeatureViewCell
    
        cell.imageIdex = indexPath.item
    
        return cell
    }
    
///    ScrollView 停止滚动调用方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //在最后一页才调用动画方法
        //根据 contentoffset 来判断页数
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page != WBNewFeatureCount - 1 {
            return
        }
        //cell 播放动画
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureViewCell
        cell.showButtonAnim()
    }
}

//MARK: - 新特性 cell
private class NewFeatureViewCell: UICollectionViewCell {

/// 图像索引
    var imageIdex: Int = 0 {
    
        didSet{
        
            iconView.image = UIImage(named: "new_feature_\(imageIdex + 1)")
            
            startButton.hidden = true

        }
    }
    
/// 添加监听方法
    @objc private func clickStartBtn() {
    
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification,
            object: nil)
    }
    
    /// 动画显示开始按钮
    private func showButtonAnim() {
    
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(2.0,
            delay: 0,
            usingSpringWithDamping: 1.6,        // 弹力系数
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
 /// 添加控件
    private func setUpUI() {
    
       addSubview(iconView)
        addSubview(startButton)
        
        //指定位置
        iconView.frame = bounds
        
        //自动布局
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        //添加监听方法
        startButton.addTarget(self, action: "clickStartBtn", forControlEvents: .TouchUpInside)
    }
    
/// 懒加载控件
    //新特性图像
    private lazy var iconView: UIImageView = UIImageView()
/// 开始体验按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), backImageName: "new_feature_finish_button")
}
